package controllers

import (
	"demo/models"
	"demo/utils/constant"
	"fmt"
	"github.com/astaxie/beego"
)

type BaseController struct {
	beego.Controller
	IsLogin bool
	Loginuser interface{}
	sessionName string
}

func (base *BaseController) Prepare()  {
	loginuser := base.GetSession("loginuser")
	fmt.Println("loginuser -----> ", loginuser)
	base.sessionName = "userInfo"
	if loginuser != nil {
		base.IsLogin = true
		base.Loginuser = loginuser
	}else{
		base.IsLogin = false
	}
	base.Data["IsLogin"] = base.IsLogin
}

//设置当前用户会话
func (base *BaseController) SetSessionUser(user *models.Users)  {
	base.SetSession(base.sessionName, user)
}

//获取当前用户会话
func (base *BaseController) GetSessionUser() *models.Users  {
	user := base.GetSession(base.sessionName)
	if user != nil {
		return user.(*models.Users)
	}
	return nil
}

//清除当前用户会话
func (base *BaseController) ClearSessionUser()  {
	if base.GetSessionUser() != nil {
		base.DelSession(base.sessionName)
	}
}

//获取系统当前用户
func (base *BaseController) GetUserName() string  {
	user := base.GetSessionUser()
	if user != nil {
		return user.Username
	}
	return constant.SYS
}
